package com.sogoodsoft.service.common;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.sogoodsoft.dao.common.CommonHibernateDao;

@Component
@Transactional
public abstract class AbstractService<T,PK extends Serializable> {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Transactional(readOnly = true)
	public T get(PK id){
		return (T)getDao().get(id);
	}

	public void save(T t){
		getDao().save(t);
	}
	
	public void delete(T t){
		getDao().delete(t);
	}
	public void delete(PK id){
		T t = getDao().get(id);
		delete(t);
	}
	
	@SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
	public <P> List<T> find(String hql,P...values){
		return (List<T>)getDao().find(hql, values);
	}
	
	
	@Transactional(readOnly = true)
	public Page<T> findPage(final Page<T> page, final List<PropertyFilter> filters) {		
		return getDao().findPage(page, filters);
	}
	
	
	/**
	 * 命名查询
	 * @param key 查询语句名
	 * @param requestMap 字符串类型的参数，键值对
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List keyQuery(String key,Map paras){
		return getDao().keyQuery(key, paras);
	}
	
	/**
	 * 命名查询，支持分页
	 * @param key 查询语句名
	 * @param page 存储分页值的分页对象
	 * @param requestMap 字符串类型的参数，键值对
	 * @return
	 * @see 查看HQL命名配置{@link com.sogoodsoft.dao.common.CommonHibernateDao}
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Page keyQuery(String key,Page page,Map paras){
		return getDao().keyQuery(key,page, paras);
	}
	
	public abstract CommonHibernateDao<T,PK> getDao();
	
	public long getSequenceVal(String seq){
		return this.getDao().getSequenceVal(seq);
	}
}
